package com.doge.walknovel.business.novel.viewholders

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.doge.walknovel.api.model.NovelDetailInfo
import com.doge.walknovel.business.novel.MIGHT_LIKE
import com.doge.walknovel.core.base.adapters.AdapterCallback
import com.doge.wnpro.databinding.CellNovelMightLikeBinding
import com.doge.walknovel.utils.setOnClickCallback

class NovelMightLikeViewHolder(
    parent: ViewGroup,
    callback: AdapterCallback,
    private val binding: CellNovelMightLikeBinding = CellNovelMightLikeBinding.inflate(
        LayoutInflater.from(parent.context), parent, false),
) : RecyclerView.ViewHolder(binding.root) {
    private val books by lazy { arrayOf(binding.like1, binding.like2, binding.like3, binding.like4, binding.like5, binding.like6) }

    init {
        books.forEach {
            it.setOnClickCallback { view ->
                callback.invoke(MIGHT_LIKE, view.tag)
            }
        }
    }

    fun bind(data: NovelDetailInfo) {
        itemView.tag = data
        val item = data.likeList ?: listOf()

        books.forEachIndexed { index, view ->
            view.visibility = if (index < item.size) View.VISIBLE else View.GONE
            if (index < item.size) {
                view.tag = item[index]
                view.set(item[index])
            }
        }
    }
}